Add IniConfig.parse() with inline comment stripping and Unicode whitespace handling#70
Merged
RonnyPfannschmidt merged 3 commits intopytest-dev:mainfrom Oct 18, 2025
Merged
Conversation
Fixes pytest-dev#55 - Inline comments were incorrectly included in parsed values The bug: Inline comments (# or ;) were being included as part of values instead of being stripped, inconsistent with how section comments are handled. Example of the bug: name = value # comment Result was: "value # comment" (incorrect) Should be: "value" (correct) Changes: - Add IniConfig.parse() classmethod with strip_inline_comments parameter - Default: strip_inline_comments=True (correct behavior - strips comments) - Can set strip_inline_comments=False if old buggy behavior needed - IniConfig() constructor preserves old behavior for backward compatibility (calls parse_ini_data with strip_inline_comments=False) - Add parse_ini_data() helper in _parse.py to avoid code duplication - Update _parseline() to support strip_inline_comments parameter - Add comprehensive tests for both correct and legacy behavior Backward compatibility: Existing code using IniConfig() continues to work unchanged. Users should migrate to IniConfig.parse() for correct behavior. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Add opt-in Unicode whitespace stripping for section names (issue pytest-dev#4) Changes: - Add strip_section_whitespace parameter to IniConfig.parse() - Default: False (preserves backward compatibility) - When True: strips Unicode whitespace from section names - Document Unicode whitespace handling in CHANGELOG - Python 3's str.strip() has handled Unicode since Python 3.0 (2008) - iniconfig 2.0.0+ benefits from this automatically - Values and key names already strip Unicode whitespace correctly - Add tests for Unicode whitespace handling Background: Since iniconfig moved to Python 3 only in version 2.0.0, all strings are Unicode by default. Python 3's str.strip() handles Unicode whitespace characters (NO-BREAK SPACE, EN QUAD, IDEOGRAPHIC SPACE, etc.) automatically. This addresses the core concern in issue pytest-dev#4 for values and key names. The new strip_section_whitespace parameter provides opt-in stripping for section names, which were not previously stripped for backward compatibility. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Consolidate __init__ to accept optional _sections and _sources parameters, allowing parse() to simply call the constructor. Changes: - Add _sections and _sources optional parameters to __init__ - Compute sections and sources first, then assign once to Final attributes - When pre-parsed data provided, use it directly (called from parse()) - Otherwise, parse the data normally (backward compatible path) - Simplify parse() to just call constructor with pre-parsed data This makes the code cleaner and easier to understand while maintaining the exact same functionality and backward compatibility. All 49 tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds backward-compatible solutions for issue #55 (inline comment handling) and issue #4 (Unicode whitespace).
Changes
1. Add
IniConfig.parse()classmethod (Fixes #55)parse()classmethod withstrip_inline_commentsparameterTrue- properly strips inline comments from valuesFalse- preserves old behavior if neededIniConfig()constructor maintains backward compatibility (doesn't strip comments)IniConfig.parse()for correct comment handlingExample:
2. Add
strip_section_whitespaceparameter (Addresses #4)IniConfig.parse()to strip Unicode whitespace from section namesFalse- preserves existing behaviorTrue: strips Unicode whitespace (U+00A0, U+2000, U+3000, etc.) from section namesstr.strip()has handled Unicode whitespace since Python 3.0 (2008)Example:
3. Code Refactoring
__init__to accept optional_sectionsand_sourcesparametersparse()to call constructor with pre-parsed data__new__logic for cleaner, more maintainable codeparse_ini_data()helper function to eliminate code duplicationTesting
Documentation
Closes
Fixes #55
Addresses #4 (opt-in solution with full Unicode whitespace documentation)
🤖 Generated with Claude Code